//
//  DFMainTabBar.swift
//  FlyBlog
//
//  Created by FEI on 16/4/8.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit

class DFMainTabBar: UITabBar {

    //定义闭包
    var composeClosure:(() -> ())?

    override func layoutSubviews() {
        super.layoutSubviews()

        let width = self.frame.size.width / 5
        //五个按钮,计算mainButton的frame
//        mainButton.frame = CGRect(origin: CGPointZero, size: CGSize(width: width, height: 49))

        var index = 0
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subView.frame = CGRectMake(CGFloat(index)*width, 0, width, frame.size.height)
//                index += index == 1 ? 2 : 1
                index++
                if index == 2 {
                    index++
                }
            }
        }
        //设置mainButton的frame
        mainButton.frame = CGRectMake(2 * width, 0, width, frame.size.height)

    }

    //MARK: -按钮点击事件
    func didClickMainButton(){
        composeClosure?()
    }

    private lazy var mainButton: UIButton = {
        let button = UIButton()

        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState:UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState:UIControlState.Highlighted)

        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState:UIControlState.Highlighted)

        button.addTarget(self, action: Selector("didClickMainButton"), forControlEvents: UIControlEvents.TouchUpInside)

        self.addSubview(button)

        return button
    }()



}
